package com.example.Service;

import com.example.PO.Product;

import java.util.ArrayList;
import java.util.List;

public class ProductService {
    private List<Product> products = new ArrayList<>();

    public ProductService() {
        // 初始化示例数据
        products.add(new Product(1, "标准螺丝", "钢", "5mm", "20mm", "十字", "平头", "10g", 100, 0.5, 1.0, 50.0, 100.0, "供应商A"));
    }

    public List<Product> getAllProducts() {
        return new ArrayList<>(products); // 返回副本防止外部直接修改
    }

    public void addProduct(Product product) {
        // 自动生成唯一的产品ID
        int maxId = products.stream()
                .mapToInt(Product::getScrewId)
                .max()
                .orElse(0);
        product.setScrewId(maxId + 1);
        products.add(product);
    }

    public void updateProduct(Product updatedProduct) {
        for (int i = 0; i < products.size(); i++) {
            Product p = products.get(i);
            if (p.getScrewId() == updatedProduct.getScrewId()) {
                products.set(i, updatedProduct);
                break;
            }
        }
    }

    public void deleteProduct(int screwId) {
        products.removeIf(p -> p.getScrewId() == screwId);
    }
}